<?php
// @formatter:off
/**
 * @file WCHolder.abs.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-04-30 02:04:27 +0000 (Tue, 30 Apr 2013) $
 *
 * $Id: WCHolder.abs.php 63 2013-04-30 02:04:27Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/holders/WCHolder.abs.php $
 */
// @formatter:on

/**
 * @class WCHolder
 *
 * This is the specification for a generic holder. It holds the logic to manage
 * list of object, preferable WCGTableRepresentation objects indexed by name and
 * id.
 *
 * @warning Every object called item should two basic properties:
 *  - name
 *  - id
 */
abstract class WCHolder {
	/**
	 * This is the class name of the items to be held.
	 *
	 * @var string
	 */
	protected $_PropertiesItemClass = "";
	/**
	 * This is the fields' prefix in the table where every held item exists.
	 * It is similar to WCGTableRepresentation::$_PropertiesPrefix
	 *
	 * @var string
	 */
	protected $_PropertiesPrefix = "";
	/**
	 * This is the table where every held item exists.
	 * It is similar to WCGTableRepresentation::$_PropertiesTable
	 *
	 * @var string
	 */
	protected $_PropertiesTable = "";
	/**
	 * @var string
	 */
	protected $_PropertiesNameColumn = "name";
	/**
	 *
	 */
	protected $_PropertiesDupNames = false;
	/**
	 * Pointer to the global connection object.
	 *
	 * @var WCConnection
	 */
	protected $_db = null;
	/** @var string */
	protected $_dbprefix = "";
	/**
	 * @var array
	 */
	protected $_items = array();
	/**
	 * @var bool
	 */
	protected $_fullLoaded = false;

	/**
	 * Constructor is protected because all holders must be singletons.
	 * Therefore, each subclass have to implemente a way to create and call
	 * the sole instance. For example, using something like this:
	 * @code
	 * protected static $_Instance;
	 *
	 * public static function Instance() {
	 * 	if(!isset(self::$_Instance)) {
	 * 		$c = __CLASS__;
	 * 		self::$_Instance = new $c();
	 * 	}
	 *
	 * 	return self::$_Instance;
	 * }
	 * @endcode
	 */
	protected function __construct() {
		global $wcDatabaseAccess;

		$this->_db = WCConnection::Instance();
		$this->_dbprefix = $wcDatabaseAccess['prefix'];
	}
	public function __destruct() {
		unset($this->_items);
	}
	/**
	 * Prevent users to clone the instance.
	 */
	public function __clone() {
		trigger_error(__CLASS__.": Clone is not allowed.", E_USER_ERROR);
	}
	//
	// Public methods
	// To implement...
	// public function create(...) {
	// public function item($id, ...) {
	// public function itemByName($name, ...) {
	// public function itemIds(...) {
	// public function items(...) {
	public function resetCache() {
		unset($this->_items);
		$this->_items = array();
	}
	//
	// Protected Methods
	protected function addToCache(&$item) {
		if(!isset($this->_items["byid"])) {
			$this->_items["byid"] = array();
			$this->_items["byname"] = array();
		}
		$this->_items["byid"][$item->id] = $item;
		eval("\$this->_items[\"byname\"][\$item->{$this->_PropertiesNameColumn}] = \$item;");
	}
	// To implement...
	//protected function findItem($id, ...) {
	//protected function findItemByName($name, ...) {

	//
	// Public Class Methods
}
?>